#include "log_api.h"
#include "sound_app.h"
#include "event_app.h"
#include "memroy_app.h"
#include "battery_app.h"
#include "basic_board.h"
#include "protocol_app.h"
#include "device_parameter.h"

/**
 * @brief 事件处理中心
 *
 */
static uint16_t BasicEventTaskCallback(uint8_t TaskId, uint16_t Events);

static uint8_t BasicEventTaskId = 0;

static BasicExternalInterfaces_t ExternalInterfaces = {0};

static uint16_t BasicEventTaskCallback(uint8_t TaskId, uint16_t Events)
{
  if (TaskId == BasicEventTaskId)
  {
    if (Events & BASIC_MOTOR_LISTER)
    {
      if (ExternalInterfaces.MotorTaskCallback != NULL)
      {
        ExternalInterfaces.MotorTaskCallback();
      }

      return (Events ^ BASIC_MOTOR_LISTER);
    }

    if (Events & BASIC_EVENT_LED_LISTER)
    {
      if (ExternalInterfaces.EventLedTaskCallback != NULL)
      {
        ExternalInterfaces.EventLedTaskCallback();
      }

      return (Events ^ BASIC_EVENT_LED_LISTER);
    }

    if (Events & BASIC_ALARM_LED_LISTER)
    {
      if (ExternalInterfaces.ErrorLedTaskCallback != NULL)
      {
        ExternalInterfaces.ErrorLedTaskCallback();
      }

      return (Events ^ BASIC_ALARM_LED_LISTER);
    }
  }

  return 0;
}

void BasicDelayMs(uint32_t Ms)
{
  DelayMs(Ms);
}

bool BasicGetExternalValidationFunctionEnableBit(void)
{
  return _EXTERNAL_VALIDATION_EN_;
}

uint16_t BasicGetMoTorDriveTime(void)
{
  return GetMotorDriveTime() * 10;
}

uint16_t BasicGetUnlockHoldTime(void)
{
  uint16_t UnlockTime = GetUnlockTime();
  if ((UnlockTime < MIX_UNLOCK_TIME) || (UnlockTime > MAX_UNLOCK_TIME))
  {
    UnlockTime = DEFAULT_UNLOCKING_TIME;
  }

  return UnlockTime * 1000;
}

bool BasicGetDeviceActivationStatus(void)
{
  uint8_t WorkMode = GetDeviceInitStatus() & (~FIRMWARE_UPGRADE);
  if (WorkMode == NORMAL_MODE)
    return true;

  return false;
}

bool BasicGetTheKeyToResetEn(void)
{
  return GetTheKeyToResetEn();
}

uint8_t BasicGetAntiSkidDetetionInterval(void)
{
  return GetTheBreakInDetcetionInterval();
}

uint8_t BasicGetLockedInReminderInterval(void)
{
  return GetLockedInReminderInterval();
}

uint8_t BasicGetFalseLockDetcetionInterval(void)
{
  return GetFalseLockDetcetionInterval();
}

uint8_t BasicGetNormalOpenReminderInterval(void)
{
  return GetNormalOpenReminderInterval();
}

bool BasicGetAntiSkidDetetionAlarmEn(void)
{
  if (GetTheBreakInDetcetionInterval() != 0)
    return true;

  return false;
}

bool BasicGetDoorLeftAjarAlarmEn(void)
{
  if (GetFalseLockDetcetionInterval() != 0)
    return true;

  return false;
}

bool BasicGetLockedInSignalOutputEn(void)
{
  if (GetLockedInReminderInterval() != 0)
    return true;

  return false;
}

bool BasicDetermineWhetherTheNetworkIsAttached(void)
{
  if (EventGetNetworkConnectStatus() == NETWORK_LINKS_BEING_ESTABLISHED)
    return true;

  return false;
}

uint8_t BasicGetRemainingBatteryPercentage(void)
{
  return BatteryGetVoltagePercentage();
}

bool BasicExecuteFactorySetting(void)
{
  if (EventStartFactorySettingTask() == true)
  {
    EventHintParameterSetStatus(true);
    return true;
  }

  EventHintParameterSetStatus(false);
  return false;
}

void BasicBeveledLatchReset(void)
{
  EventExitExecuteUnlockTask();
}

void BasicAntiSkidReturnedToDefaultState(void)
{
  EventExitPickLockTipTask();
}

void BasicAntiSkidTiggerAlarmState(void)
{
  EventStartPickLockTipTask();
}

void BasicKeyRemove(void)
{
  EventExitKeyInstallTipTask();
}

void BasicKeyInsert(void)
{
  EventStartKeyInstallTipTask();
}

void BasicLockedInTigger(void)
{
  EventStartLockedInTipTask();
}

bool BasicGetUnlockTaskIsAction(void)
{
  return EventGetActionTriggerStatus(DEV_UNLOCK);
}

void BasicLockInRecovery(void)
{
  EventExitLockedInTipTask();
}

void BasicFalseReturnedToDefaultState(void)
{
  EventExitFalseLockedTipTask();
}

void BasicFalseTiggerAlarmState(void)
{
  EventStartFalseLockedTipTask();
}

void BasicPlayDeviceInTheInitializedStateAudio(void)
{
  PlayDeviceInTheInitializedStateAudio();
}

void BasicPlayLowBatteryAudio(void)
{
  PlayLowBatteryAudio();
}

void BasicWriteKeyedInTiggerStatusLogFile(bool TiggerEn)
{
  if (TiggerEn == true)
  {
    uint16_t WriteTableId = WriteKeyUnlockLog();
    if (WriteTableId != 0)
    {
      EventStartAlarmReportingTask(WriteTableId, EVENT_ALARM_KEY_IN, TiggerEn);
    }
  }
}

void BasicWriteAutiPickAlarmTiggerStatusLogFile(bool TiggerEn)
{
  uint16_t WriteTableId = WriteAntiSkidAlarmLog(TiggerEn);
  if (WriteTableId != 0)
  {
    EventStartAlarmReportingTask(WriteTableId, EVENT_ALARM_ANTI_SKID, TiggerEn);
  }
}

void BasicWriteFalseLockAlarmTiggerStatusLogFile(bool TiggerEn)
{
  uint16_t WriteTableId = WriteFalseLookAlarmLog(TiggerEn);
  if (WriteTableId != 0)
  {
    EventStartAlarmReportingTask(WriteTableId, EVENT_ALARM_FALSE_LOCK, TiggerEn);
  }
}

void BasicWriteDoubleLockTiggerStatusLogFile(bool TiggerEn)
{
  uint16_t WriteTableId = WriteDoubleLookAlarmLog(TiggerEn);
  if (WriteTableId != 0)
  {
    EventStartAlarmReportingTask(WriteTableId, EVENT_ALARM_DOUBLE_LOCK, TiggerEn);
  }
}

void BasicWriteDoorUnlockLogFile(bool TiggerEn)
{
}

void BasicStopMotorTask(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_MOTOR_LISTER);
}

void BasicDelayedStartMotorTask(uint32_t DelayMs)
{
  tmos_stop_task(BasicEventTaskId, BASIC_MOTOR_LISTER);
  tmos_start_task(BasicEventTaskId, BASIC_MOTOR_LISTER, DelayMs);
}

void BasicStartMotorTaskNoWait(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_MOTOR_LISTER);
  tmos_set_event(BasicEventTaskId, BASIC_MOTOR_LISTER);
}

void BasicStopEventLedTask(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_EVENT_LED_LISTER);
}

void BasicDelayedStartEventLedTask(uint32_t DelayMs)
{
  tmos_stop_task(BasicEventTaskId, BASIC_EVENT_LED_LISTER);
  tmos_start_task(BasicEventTaskId, BASIC_EVENT_LED_LISTER, DelayMs);
}

void BasicStartEventLedTaskNoWait(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_EVENT_LED_LISTER);
  tmos_set_event(BasicEventTaskId, BASIC_EVENT_LED_LISTER);
}

void BasicStopAlarmLedTask(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_ALARM_LED_LISTER);
}

void BasicDelayedStartAlarmLedTask(uint32_t DelayMs)
{
  tmos_stop_task(BasicEventTaskId, BASIC_ALARM_LED_LISTER);
  tmos_start_task(BasicEventTaskId, BASIC_ALARM_LED_LISTER, DelayMs);
}

void BasicStartAlarmLedTaskNoWait(void)
{
  tmos_stop_task(BasicEventTaskId, BASIC_ALARM_LED_LISTER);
  tmos_set_event(BasicEventTaskId, BASIC_ALARM_LED_LISTER);
}

void BasicBoardInitialisation(BasicExternalInterfaces_t *pExternalInterfacesIn)
{
  LedBoardInitialisation();
  MotorBoardInitialisation();
  SensorBoardInitialisation();

  ExternalInterfaces.MotorTaskCallback = pExternalInterfacesIn->MotorTaskCallback;
  ExternalInterfaces.ErrorLedTaskCallback = pExternalInterfacesIn->ErrorLedTaskCallback;
  ExternalInterfaces.EventLedTaskCallback = pExternalInterfacesIn->EventLedTaskCallback;
  BasicEventTaskId = TMOS_ProcessEventRegister(BasicEventTaskCallback);
}
